package pri.damai.xiaowu.common.core.vo;

import pri.damai.xiaowu.common.core.enums.ResultCommonEnum;
import pri.damai.xiaowu.common.core.enums.base.StringEnumerator;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

/**
 * 统一返回结果类
 * @Desc
 * @Author DaMai
 * @Date 2021/3/23 15:40
 * 但行好事，莫问前程。
 */
@Data
public class ResultVO<T> implements Serializable {

    @Schema(description = "业务码")
    private String code;
    @Schema(description = "业务消息")
    private String msg;
    @Schema(description = "业务数据")
    private T data;

    public Boolean isSuccess() {
        return Objects.equals(this.code, ResultCommonEnum.SUCCESS.code());
    }

    /**
     * 有参成功
     * @apiNote [附加描述]
     * @param data 业务数据
     * @return com.ruiruan.userserver.vo.ResultVO<T>
     * @author DaMai
     * @date 2021/3/23 15:41
     */
    public static <T> ResultVO<T> success(T data) {
        ResultVO<T> resultVO = new ResultVO<T>();
        resultVO.setCode(ResultCommonEnum.SUCCESS.code());
        resultVO.setMsg(ResultCommonEnum.SUCCESS.desc());
        resultVO.setData(data);
        return resultVO;
    }

    /**
     * 无参成功
     * @apiNote [附加描述]
     * @return com.ruiruan.userserver.vo.ResultVO<T>
     * @author DaMai
     * @date 2021/3/23 15:41
     */
    public static <T> ResultVO<T> success() {
        return success(null);
    }

    /**
     * 有参错误
     * @apiNote [附加描述]
     * @param code 错误码
     * @param msg 错误消息
     * @return com.ruiruan.userserver.vo.ResultVO<T>
     * @author DaMai
     * @date 2021/3/23 15:41
     */
    public static <T> ResultVO<T> error(String code, String msg) {
        ResultVO<T> resultVO = new ResultVO<>();
        resultVO.setCode(code);
        resultVO.setMsg(msg);
        return resultVO;
    }

    /**
     * 有参错误
     * @apiNote [附加描述]
     * @param resultEnum 错误枚举
     * @return com.ruiruan.userserver.vo.ResultVO<T>
     * @author DaMai
     * @date 2021/3/23 15:41
     */
    public static <T> ResultVO<T> error(StringEnumerator resultEnum) {
        return error(resultEnum.code(), resultEnum.desc());
    }
}
